package com.libgdxbar.pvzlikegame.objects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ObjectMap;
import com.libgdxbar.pvzlikegame.asset.AssetBatch;
import com.libgdxbar.pvzlikegame.asset.AssetsControl;
import com.libgdxbar.pvzlikegame.asset.AssetsLoader;
import com.libgdxbar.pvzlikegame.entities.MoveableEntity;
import org.libgdxbar.projects.interfaces.asset.AssetBatchCallback;
import org.libgdxbar.projects.interfaces.renderer.BatchRenderable;

import java.util.Objects;

public class SampleTextureAsync extends MoveableEntity implements BatchRenderable, Disposable {

    public static String TAG = SampleTextureAsync.class.getSimpleName();
    private Texture libgdx;
    private Texture plant;

    private Texture zombie;
    private final AssetsLoader loader = AssetsControl.getLoader(getClass());

    public SampleTextureAsync(float x, float y) {
        // 演示逻辑 创建随机方向移动的纹理
        super(x, y, MathUtils.random(200f) + 200f, MathUtils.random(200f) + 200f);

        // 演示异步获取资源
        loader.get("libgdx.png", Texture.class, asset -> SampleTextureAsync.this.libgdx = asset);

        // 演示同步获取资源
        plant = loader.get("plant-stub.png", Texture.class);

        // 构建资源批量获取器
        AssetBatch assetBatch = AssetBatch.builder()
                // 添加一个文件
                .add("zombie-stub.png", Texture.class)
                // 匹配所有png文件
                .pattern(".*\\.png", Texture.class)
                .build();
        // 演示异步批量获取资源
        loader.get(assetBatch, new AssetBatchCallback() {
                    @Override
                    public void process(int progress, int total) {
                        // 打印进度
                        Gdx.app.log(TAG, "progress:" + progress + " total:" + total + " " + (int) ((progress * 1f / total) * 100) + "%");
                    }

                    @Override
                    public void finish() {
                        Gdx.app.log(TAG, "异步批量获取资源完成");

                        // 同步获取单个资源（已加载，直接返回）
                        zombie = loader.get("zombie-stub.png", Texture.class);

                        // 打印所有已加载资源
                        ObjectMap<String, ObjectMap<Class<?>, Object>> assets = loader.getAssets();
                        for (ObjectMap.Entry<String, ObjectMap<Class<?>, Object>> entry : assets.entries()) {
                            Gdx.app.log(TAG, "文件名:" + entry.key);
                            for (ObjectMap.Entry<Class<?>, Object> asset : entry.value.entries()) {
                                Gdx.app.log(TAG, "类型:" + asset.key + " 资源:" + asset.value);
                            }
                        }
                    }
                }
        );
    }

    @Override
    public void run(float delta) {
        super.run(delta);
        Vector2 position = getPosition();
        // 这里碰屏幕边缘反弹只是演示逻辑而已 实际请自己实现
        if (position.x > 640 - 150) setVelocityX(-Math.abs(getVelocityX()));
        if (position.x < 0) setVelocityX(Math.abs(getVelocityX()));
        if (position.y > 480 - 100) setVelocityY(-Math.abs(getVelocityY()));
        if (position.y < 0) setVelocityY(Math.abs(getVelocityY()));
    }

    @Override
    public void render(Batch batch) {

        Vector2 position = getPosition();
        if (Objects.nonNull(libgdx)) {
            batch.draw(libgdx, position.x, position.y, 150, 50);
        }
        if (Objects.nonNull(plant)) {
            batch.draw(plant, position.x, position.y + 50, 150, 50);
        }
    }

    @Override
    public void dispose() {
        // 应当及时释放资源
        loader.dispose();
    }
}
